//
//  ParametersTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2022/8/4.
//

import UIKit

class ParametersTableViewCell: UITableViewCell {
    var titleLab: UILabel!
    var collectionView: UICollectionView!
    var moreSpecificationArray: [ParametersModel] = []//多规格
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.clipsToBounds = true
        
        self.initUI()
        self.addLayout()
        
    }
    
    func initUI() {
   
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 150), collectionViewLayout: layout)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = .white
        self.collectionView.register(SkuCollectionViewCell.self, forCellWithReuseIdentifier: "SkuCollectionViewCell")
        self.collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 14, bottom: 0, right: 14)
        self.collectionView.isScrollEnabled = false
        self.contentView.addSubview(self.collectionView)
    }
    
    func addLayout() {
        
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.greaterThanOrEqualTo(40)
            make.bottom.equalTo(0)
        }
        
    }
    
    
    func setDataArray(dataArray: [ParametersModel]){
        self.moreSpecificationArray = dataArray
        self.collectionView.reloadData()
        super.layoutIfNeeded()
        let hh = self.collectionView.collectionViewLayout.collectionViewContentSize.height
        self.collectionView.snp.updateConstraints { make in
            make.height.equalTo(hh)
        }
        self.updateConstraints()
    }
   
 
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension ParametersTableViewCell: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
        
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SkuCollectionViewCell", for: indexPath) as! SkuCollectionViewCell
        if self.moreSpecificationArray.count > indexPath.row{
            let model = self.moreSpecificationArray[indexPath.row]
            cell.titleLab.text = model.name ?? ""
            cell.bgView.backgroundColor = .white
        }
     
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
        return CGSize.init(width: (kScreenWidth - 2*14 - 5 * 2)/3, height: 32)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.moreSpecificationArray.count > indexPath.row{
//            let model = self.dataArray[indexPath.row] as! StoreListDataModel
//            let vc =  ProductDetailsViewController()
//            vc.storeListModel = model
//            self.viewController().navigationController?.pushViewController(vc, animated: true)
        }
       
    }

}


